package cn.tedu.charging.device.repository;

import cn.tedu.charging.common.pojo.po.ChargingGunInfoPO;
import cn.tedu.charging.common.pojo.po.ChargingStationPO;
import cn.tedu.charging.common.pojo.query.NearStationsQuery;
import cn.tedu.charging.device.mapper.GunMapper;
import cn.tedu.charging.device.pojo.po.StationCanalPO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.*;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.GeoOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Slf4j
public class DeviceRepositoryImpl implements DeviceRepository{
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private GunMapper gunMapper;

    //保留 其他数据 持久层 数据库的mapper注入
    @Override
    public List<GeoResult> nearStations(NearStationsQuery query) {
        //georadius lng lat radius km withcoord withdist
        //1.准备好缓存预热写入的geokey值charging:all:stations
        String geoKey = "charging:all:stations";
        //2.选项 withdist withcoord
        RedisGeoCommands.GeoRadiusCommandArgs args
                = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs();
        args.includeCoordinates().includeDistance();
        //3.操作geo类型
        GeoOperations geoOps = redisTemplate.opsForGeo();
        //4.封装参数圆圈范围circle
        Point center=new Point(query.getLongitude(),query.getLatitude());
        Distance radius=new Distance(query.getRadius(),RedisGeoCommands.DistanceUnit.KILOMETERS);
        Circle circle=new Circle(center,radius);
        GeoResults geoResults = geoOps.radius(geoKey, circle, args);
        return geoResults.getContent();
    }

    @Override
    public String getStationName(Integer stationId) {
        //get key
        //1.操作string的子对象
        ValueOperations<String,ChargingStationPO> stringOps = redisTemplate.opsForValue();
        //2.从缓存直接获取po
        ChargingStationPO poCache=stringOps.get("charging:stations:detail:"+stationId);
        //statinPO.getStationName
        if (poCache!=null){
            //缓存命中
            return poCache.getStationName();
        }
        return "";
    }

    @Override
    public ChargingStationPO getStationById(Integer stationId) {
        //查询缓存
        String stationKey="charging:stations:detail:"+stationId;
        ValueOperations<String,ChargingStationPO> stringOps =
                redisTemplate.opsForValue();
        return stringOps.get(stationKey);
    }

    @Override
    public List<ChargingGunInfoPO> getGunListByStationId(Integer stationId) {
        return gunMapper.selectByStationId(stationId);
    }

    @Override
    public Boolean updateGunStatus(Integer gunId, Integer status, List<Integer> integers) {
        return null;
    }

    @Override
    public void saveStation(StationCanalPO stationCanalPO) {
        String stationKey="charging:stations:detail:"+stationCanalPO.getId();
        ValueOperations<String,ChargingStationPO> stringOps =
                redisTemplate.opsForValue();
        ChargingStationPO chargingStationPO = new ChargingStationPO();
        BeanUtils.copyProperties(stationCanalPO,chargingStationPO);
        //set 有更新 没有 插入
        stringOps.set(stationKey,chargingStationPO);
    }

    @Override
    public void updateStation(StationCanalPO before, StationCanalPO after) {
        //删除缓存
        String stationKey="charging:stations:detail:"+before.getId();
        ValueOperations<String,ChargingStationPO> stringOps =
                redisTemplate.opsForValue();
        //set 有更新 没有 插入
        //before 更新前的数据 after 更新前后数据
        ChargingStationPO chargingStationPO = new ChargingStationPO();
        BeanUtils.copyProperties(after,chargingStationPO);
        stringOps.set(stationKey,chargingStationPO);
    }

    @Override
    public void deleteStation(StationCanalPO stationCanalPO) {
        //删除缓存
        String stationKey="charging:stations:detail:"+stationCanalPO.getId();
        redisTemplate.delete(stationKey);
    }
}
